package com.neuedu.service.impl;

import com.google.common.base.Splitter;
import com.neuedu.common.JSONData;
import com.neuedu.dao.ShippingMapper;
import com.neuedu.pojo.Shipping;
import com.neuedu.service.ShippingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service(value = "shippingService")
public class ShippingServiceImpl implements ShippingService {
    @Autowired
    private ShippingMapper shippingMapper;
    public JSONData<String> addShipping(Integer userId, Shipping shipping) {

        shipping.setUserId(userId);
        //-
        int num = shippingMapper.insert(shipping);
        if(num>0){
            return JSONData.buildSuccess("收货地址添加成功");
        }
        return JSONData.buildError("收货地址添加失败");
    }

    public JSONData<String> updateShippingByIdAndUserId(Integer userId, Shipping shipping) {
        shipping.setUserId(userId);
        int num = shippingMapper.updateByIdAndUserId(shipping);
        if(num>0){
            return JSONData.buildSuccess("收货地址修改成功");
        }
        return JSONData.buildError("收货地址修改失败");
    }

    public JSONData<Shipping> selShippingByIdAndUserId(Integer userId, Integer shippingId) {
        Shipping shipping = shippingMapper.selShippingByIdAndUserId(userId, shippingId);
        if(shipping==null){
            return JSONData.buildError("操作失败");
        }
        return JSONData.buildSuccess(shipping);
    }

    public JSONData<List<Shipping>> selAllShippingByUserId(Integer userId) {
        List<Shipping> shippings = shippingMapper.selAllShippingByUserId(userId);
        //-根据需求判断是否增加此代码
        if(shippings==null){
            return JSONData.buildError("操作失败");
        }
        return JSONData.buildSuccess(shippings);
    }

    public JSONData<List<Shipping>> delShippingByShippingIds(Integer userId, String shippingIds) {
        //-shippingIds 字符串的拆分
        List<String> shippingIdList = Splitter.on(",").splitToList(shippingIds);
        if(shippingIdList==null){
            return JSONData.buildError("操作失败");
        }
        int num = shippingMapper.delShippingByShippingIds(userId, shippingIdList);
        if(num==0){
            return JSONData.buildError("操作失败");
        }
        //-:删除成功之后，将剩余的信息，返回回去
        List<Shipping> shippings = shippingMapper.selAllShippingByUserId(userId);
        return JSONData.buildSuccess(shippings);
    }
}
